﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageBidCategories : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_BidCATEGORY = "__BidCategory::DC";
    private const string PROP_LAYOUT = "__BidCategory::UIL";
    private const string PROP_SELECTED_BidCATEGORY_ID = "__BidCategory::SDCID";

    #endregion

    #region Public Properties

    public BidCategoryCollection DataSourceBidCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BidCATEGORY] is BidCategoryCollection ? (BidCategoryCollection)SessionMgr.Session[PROP_DATASRC_BidCATEGORY] : (BidCategoryCollection)(SessionMgr.Session[PROP_DATASRC_BidCATEGORY] = new BidCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BidCATEGORY] = value; }
    }

    public int SelectedBidCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_BidCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_BidCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_BidCATEGORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceBidCategory = BidCategory.GetAllBidCategories();
            BidCategoryList1.DataBind(DataSourceBidCategory);
        }
    }

    #endregion

    #region Control: BidCategoryList

    protected void ucBidCategoryList_EditRequested(object sender, Controls_BidCategoryList.EditRequestedEventArgs evt)
    {
        SelectedBidCategoryID = evt.BC.BidCategoryID;
        BidCategoryViewEdit1.DataBind(evt.BC);
        SetLayout(LayoutType.Edit);
    }


    protected void ucBidCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        BidCategoryViewEdit1.CancelEdit();
        BidCategory newBidCategory = BidCategory.CreateNew(DataSourceBidCategory);
        SelectedBidCategoryID = newBidCategory.BidCategoryID;
        BidCategoryViewEdit1.DataBind(DataSourceBidCategory.FindByBidCategoryID(SelectedBidCategoryID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucBidCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceBidCategory = BidCategory.GetAllBidCategories();
        BidCategoryList1.DataSource = DataSourceBidCategory;
    }

    #endregion

    #region Control: BidCategoryViewEdit

    protected void ucBidCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        BidCategoryViewEdit1.DataSourceBidCategory = DataSourceBidCategory.FindByBidCategoryID(SelectedBidCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        BidCategoryViewEdit1.CancelEdit();
        // If we were adding a new jobcategory...then remove the incomplete/not added new jobcategory object from the collection.
        if (DataSourceBidCategory.FindByBidCategoryID(SelectedBidCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceBidCategory.Remove(DataSourceBidCategory.FindByBidCategoryID(SelectedBidCategoryID));
                SelectedBidCategoryID = (DataSourceBidCategory[0] != null ? DataSourceBidCategory[0].BidCategoryID : 0);
            }
            catch
            {
            }
        }
        BidCategoryList1.DataBind(DataSourceBidCategory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (BidCategoryViewEdit1.EndEdit())
        {
            BidCategory obj = DataSourceBidCategory.FindByBidCategoryID(SelectedBidCategoryID);
            BidCategory.Update(obj);

            // TODO: Set Feedback text...

            BidCategoryList1.DataBind(DataSourceBidCategory);
            SetLayout(LayoutType.View);

        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlBidCategoryList.Visible = false;
                pnlBidCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlBidCategoryList.Visible = true;
                pnlBidCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion

}
